#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "symm.h"

void init_array(
    int m,
    int n,
    double *alpha,
    double *beta,
    double C[20][30],
    double A[20][20],
    double B[20][30]) {
    int i, j;

    *alpha = 1.5;
    *beta = 1.2;
    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++) {
            C[i][j] = (double)((i + j) % 100) / m;
            B[i][j] = (double)((n + i - j) % 100) / m;
        }
    for (i = 0; i < m; i++) {
        for (j = 0; j <= i; j++)
            A[i][j] = (double)((i + j) % 100) / m;
        for (j = i + 1; j < m; j++)
            A[i][j] = -999;
    }
}

void print_array(int m, int n, double C[20][30]) {
    int i, j;

    fprintf(stderr, "==BEGIN DUMP_ARRAYS==\n");
    fprintf(stderr, "begin dump: %s", "C");
    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++) {
            if ((i * m + j) % 20 == 0)
                fprintf(stderr, "\n");
            fprintf(stderr, "%0.6lf ", C[i][j]);
        }
    fprintf(stderr, "\nend   dump: %s\n", "C");
    fprintf(stderr, "==END   DUMP_ARRAYS==\n");
}

int main() {
    int m = 20;
    int n = 30;
    int has_errors = 0;

    double alpha;
    double beta;
    double C[20][30];
    double A[20][20];
    double B[20][30];

    char data_buf[256];
    char check_buf[256];

    init_array(m, n, &alpha, &beta, C, A, B);

    kernel_symm(alpha, beta, C, A, B);

    FILE *tb_file = fopen("check.data", "r");
    assert(tb_file != NULL && "Couldn't open check data file");

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            sprintf(data_buf, "%0.6lf", C[i][j]);
            int res = fscanf(tb_file, "%s", check_buf);
            assert(res == 1 && "read check data file failed");
            if (strcmp(data_buf, check_buf)) {
                printf("C[%d][%d] = %s, expected_C[%d][%d] = %s\n",
                       i, j,
                       data_buf,
                       i, j,
                       check_buf);
                has_errors++;
            }
        }
    }

    fclose(tb_file);

    if (has_errors) {
        fprintf(stderr, "Benchmark results are incorrect\n");
        return -1;
    }

    printf("Success.\n");
    return 0;
}
